package edu.cmsc611.textsimulator;

import java.util.HashMap;
import java.util.Map;

public class DataMemory {
	private static DataMemory instance=null;
	private int[][] memblock=new int[8][4];
	//int memorycapacity;
	HashMap<Integer,Integer> data=new HashMap<Integer,Integer>();

	DataMemory()
	{
		for(int i=0;i<memblock.length;i++)
			for(int j=0;j<memblock[0].length;j++)
				memblock[i][j]=-1;
	}

	public void printlnData(){
		

		//then you just access the reversedMap however you like...
		for (Map.Entry entry : data.entrySet()) {
		    System.out.println(entry.getKey() + ", " + entry.getValue());
		}
	}
	public void writetoDataMemory(int index,int value)
	{
		data.put(index, value);
	}

	public int readDataMemory(int index)
	{
		return data.get(index);
	}
	public int[] getMemAddresses(int blockindex)
	{
		return memblock[blockindex];
	}
	public int getBlockNumber(int address)
	{
		for(int i=0;i<memblock.length;i++)
			for(int j=0;j<memblock[0].length;j++)
			{
				if(memblock[i][j]==address)
					return i;

			}
		return -1;
	}
	public void setBlock(int blockindex,int address)
	{
		for(int i=0;i<memblock[blockindex].length;i++)
			if(memblock[blockindex][i]==-1){
				memblock[blockindex][i]=address;
				break;
			}
	}
	public static DataMemory getInstance(){
		if(instance==null){
			instance=new DataMemory();
		}
		return instance;
	}
}
